package com.zis.platform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import com.zis.platform.dao.DictionaryDao;
import com.zis.platform.model.Dictionary;
import com.zis.platform.model.DictionaryType;
import com.zis.platform.service.IDictionaryInter;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2017/12/25
 */
@Service
@Transactional("txManager")
public class DictionaryInterImpl extends BaseInterImpl<Dictionary> implements IDictionaryInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private DictionaryDao dictionaryDao;

    @Override
    public String insert(Dictionary dictionary) throws Exception {
        //查询字典类型是否重复
        Dictionary params = new Dictionary();
        params.setDictTypeCode(dictionary.getDictTypeCode());
        params.setDictValue(dictionary.getDictValue());
        List<Dictionary> dictList = sqlManager.template(params);
        if(dictList.size()>0){
            return ResponseEntity.createDuplicationJsonResponse(Constant.DTAT_RESULT_DUP);
        }
        dictionary.setCreateDate(new Date());
        int num = sqlManager.insert(dictionary);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(Dictionary dictionary) throws Exception {
        dictionary.setUpdateDate(new Date());
        int num = sqlManager.updateTemplateById(dictionary);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(Dictionary.class,id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public Dictionary selectById(String id) throws Exception {
        return sqlManager.unique(Dictionary.class,id);
    }

    @Override
    public List<Dictionary> findByCondition(Dictionary dictionary) throws Exception {
        return null;
    }

    @Override
    public String findDictJson() throws Exception {
        JSONObject result = new JSONObject();
        JSONObject dictObj = new JSONObject();
        JSONObject dictArray = new JSONObject();
        List<DictionaryType> dictTypeList = sqlManager.all(DictionaryType.class);
        for (DictionaryType dictType : dictTypeList){
            //组织封装明细
            JSONObject dictJson = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            Dictionary params = new Dictionary();
            params.setDictTypeCode(dictType.getDictTypeCode());
            List<Dictionary> dictList = sqlManager.template(params);
            for (Dictionary dict : dictList){
                dictJson.put(dict.getDictValue(),dict.getDictName());
                JSONObject temp = new JSONObject();
                temp.put("value", dict.getDictValue());
                temp.put("label", dict.getDictName());
                jsonArray.add(temp);
            }
            dictObj.put(dictType.getDictTypeCode(),dictJson);//对象
            dictArray.put(dictType.getDictTypeCode(),jsonArray);//数组对象
        }
        result.put("dictJSON",dictObj);
        result.put("dictArray",dictArray);
        return ResponseEntity.createNormalJsonResponse(result);
    }

    @Override
    public List<Dictionary> findByTypeCode(String dictTypeCode) throws Exception {
        return dictionaryDao.findByTypeCode(dictTypeCode);
    }
}
